/******************
变更部门
* */
<template>
  <el-dialog
    width="50%"
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="20vh"
    @close="closeDialog()"
  >
    <el-form
      ref="dataForm"
      :rules="formRules"
      :model="dataForm"
      :inline="true"
      width="130px"
      style="margin: 0 10px"
    >
      <el-row style="display: flex;justify-content: space-between;align-items:center;border: 1px solid  rgb(176, 189, 226);position: relative;height: 100px;padding-top: 20px;border-radius: 5px">
        <span  style="position: absolute;top: -10px;left: 50px;font-size: 16px;background: #FFFFFF;padding: 0 10px;color:#0e258c">原部门信息</span>
        <el-form-item  label="部门名称">
           <span>{{dataForm.assessDeptName}}</span>
        </el-form-item>
      </el-row>
      <el-row style="display: flex;justify-content: space-between;align-items:center;border: 1px solid  rgb(176, 189, 226);position: relative;height: 100px;padding-top: 20px;border-radius: 5px;margin-top: 25px">
        <span  style="position: absolute;top: -10px;left: 50px;font-size: 16px;background: #FFFFFF;padding: 0 10px;color:#0e258c">
           <el-tooltip class="item" effect="dark" content="提示：调入机构将接管原机构的全部考核指标信息" placement="top-start">
                <span>调入机构信息<i class="el-icon-question" style="margin-left:10px;color:#0e258c"></i></span>
           </el-tooltip>
        </span>
        <el-form-item label="" prop="ids">
              <el-button icon="el-icon-user-solid" @click="showDept" type="text"></el-button>
              <span v-for="it in options" :key="it.assessDeptId" >
                  <el-tooltip
                    class="item"
                    :disabled="options.length < 3"
                    effect="dark"
                    :content="shaiXuan(options, '被考核机构')"
                    placement="top-start"
                  >
                    <span style="font-size: 16px; color: #303745">{{ it.assessDeptName }}</span>
                  </el-tooltip>
               </span>
        </el-form-item>
      </el-row>
    </el-form>

    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" plain @click="handleConfirm">确定</el-button>
      <el-button type="primary" plain @click="closeDialog()">返回</el-button>
    </span>
    <targetDeptSelect v-if="deptcomment" ref="deptcomment" @submitfy="submitfy"></targetDeptSelect>

  </el-dialog>
</template>

<script>
import targetDeptSelect from "@/views/dailyExamine/components/targetDept-select";
import { changePerson } from "@/api/dailyExamine/targetExamine/perORtarChange";
import {zltest} from "@/api/dailyExamine/targetExamine/createAssessment";
export default {
  data() {
      var validateIDs = (rule, value, callback) => {
          if (this.options.length === 0) {
              callback(new Error("请选择被考核机构"));
          } else {
              zltest(
                  Object.assign(
                      {
                          assessId: this.fatherData.id, //考核周期id
                          type: 1, //考核对象类型，0人员，1机构
                          participantIds: this.getUserIds(this.options) //this.getUserIds(this.options)// 637//536//考核对象(人员或机构id) this.getUserIds(this.options)
                      },
                      ""
                  )
              )
                  .then(r => {
                      if (r.msg) {
                          callback(new Error("同一周期内被考核人不可以重复"));
                      } else {
                          callback();
                      }
                  })
                  .catch(err => {});
          }
      };
    return {
      usercomment: false, //人员选择组件显隐
      fatherData: {}, //总部启动的考核数据
      repeatDataList: [], //复用指标数据
      chooseTarget: [], //选中的指标数组
      repeatList: [], //选中的复用指标
      repeatVisible: false, //复用指标弹窗显隐
      dialogVisible: false, //考核指标库显隐
      multipleSelection: [], //选中的指标
      dataList: [], //指标数组
      deptcomment: false, //选择部门组件
      options: [], //被考核部门数组
      dialogTitle: "新增机构考核", //弹窗标题
      visible: false, //最外层弹窗显隐
      dataForm: {
        //表单对象
        ids: []
      },
      formRules: {
          //校验规则
          ids: [{ validator:validateIDs, trigger: "change" }]
      },
      chooseDeptData: {}, //选择的数据提供部门人数据
      participantList: [], //被考核部门暂存数据
      submitForm: {
        //提交给后台的数据对象
        name: null, //考核名称
        participantList: [], //被考核部门
        kpiDetailList: [], //指标
        type: 1 //类型 0是人员 1 是机构
      },
      deptArr:[]
    };
  },
  components: {
    targetDeptSelect, //部门选择组件
  },

  methods: {
    //筛选出姓名与职务
    shaiXuan(data, type) {
      var list = [],
        obj = {};
      if (type == "被考核机构") {
        for (var i in data) {
          list.push(data[i].jigouName);
        }
      }
      return list.toString();
    },
    // 数据提供人员选择的提交
    submitfyUser(data) {
      if (data.type == "shr") {
        this.dataList.map(it => {
          if (it.normId == this.chooseDeptData.normId) {
            this.$set(it, "provideRealName", data.tableDataUser[0].userName);
            this.$set(it, "provideUserName", data.tableDataUser[0].userCode);
            this.$set(it, "provideUserId", data.tableDataUser[0].userId);
            this.$set(it, "provideDeptName", data.tableDataUser[0].deptName);
            this.$set(it, "provideDeptId", data.tableDataUser[0].deptId);
          }
        });
        this.personSelectChange();
      } else {
        this.options = data.tableDataUser;
        this.dataForm.ids = data.tableDataUser.map(it => {
          return it.userId;
        });
        this.personSelectChange();
      }
    },
    // 展示数据提供部门
    showDataProvide(data) {
      this.usercomment = true;
      this.$nextTick(() => {
        this.$refs.usercomment.init(1, "shr", JSON.stringify([{}]), "isFather");
      });
      this.chooseDeptData = data;
    },
    // 选中复用数据
    handleRepeatChange(val) {
      this.repeatList = val;
    },
    // 复用指标
    repeatTarget() {
      this.repeatVisible = true;
    },
    // 添加指标
    addZhibiao() {
      this.dataList = this.chooseTarget.map(it => {
        it.type = it.normLx;
        it.name = it.normName;
        it.formula = it.normGsname;
        it.formulaId = it.normGs;
        it.completeValue = it.normGetDate;
        it.itemIndex = it.normPx;
        it.keiExplain = it.normSm;
        it.unit = it.normDw;
        return it;
      });
      this.dialogVisible = false;
    },
    // 指标组件穿过来的数据
    getRow(rows) {
      this.chooseTarget = rows;
    },
    // 显示指标库
    showTarLibrary() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.zhibiaoRef.init(1, 1);
      });
    },
    // 选中指标
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 被考核部门选择
    personSelectChange(data) {
      this.participantList = [];
      this.options.map(it => {
            this.participantList.push({
              assessDeptId: it.deptId,
              assessDeptName: it.jigouName, //机构
              assessRealName: it.userName,
              assessUserId: it.userId,
              assessUserName: it.userCode
            });
      });
      this.options = this.participantList.slice(0,1) ;
    },
    //   机构展示
    showDept() {
      this.$refs["dataForm"].resetFields();
      this.deptcomment = true;
      this.$nextTick(() => {
        this.$refs.deptcomment.init("12", "jg", JSON.stringify(this.deptArr), "htcj");
      });
    },
    // 部门组件选择后确定
    submitfy(data) {
        var arr = [] ;
        if (data.tableDataUser.length>1){
            this.$message.error("只能选择一个机构")
        }
        arr.push( data.tableDataUser.pop()); //取最后一个元素
        this.options = arr ;
        this.deptArr = arr ;
        this.personSelectChange();
    },
    // 确定
    handleConfirm() {
      let params = this.options[0] ;
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          params.id = this.dataForm.id;
          changePerson(params)
            .then(r => {
              if (r.data.code == 0) {
                this.$message.success("变更成功");
                this.$emit("closeDialog");
                this.$emit("refesData");
              }
            })
            .catch(err => {
              this.$message.error(r.data.msg);
              this.$emit("closeDialog");
              this.$emit("refesData");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 初始化
    init(data, editData) {
      this.visible = true;
      this.fatherData = data.data;
        if (editData) {
            if (editData) {
                this.dialogTitle = "变更机构";
                this.dataForm = editData;
            }
        }
    },
    // 关闭
    closeDialog() {
      this.visible = false;
      this.$emit("closeDialog");
    },
      getUserIds(userArr) {
          let ids = [];
          userArr.map(it => {
              ids.push(it.assessDeptId);
          });
          return ids.toString();
      },
  }
};
</script>

<style lang="scss">
</style>
